package com.bestcem.xm.ticket.dao.impl.mysql;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.ObjectId;
import com.bestcem.xm.common.core.uitls.DateUtil;
import com.bestcem.xm.ticket.controller.param.PageParam;
import com.bestcem.xm.ticket.dao.TemplateDao;
import com.bestcem.xm.ticket.dao.converter.TicketTemplateV2Converter;
import com.bestcem.xm.ticket.dao.mapper.TicketTemplateV2DaoMapper;
import com.bestcem.xm.ticket.entity.mongo.TicketConfig;
import com.bestcem.xm.ticket.entity.mongo.TicketTemplateV2;
import com.bestcem.xm.ticket.entity.mysql.TicketTemplateV2DO;
import com.bestcem.xm.ticket.enums.TemplateUsedEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;


/**
 * @author zhangfeng <feng.zhang@idiaoyan.com>
 * @version v1.0
 * @date 2021/9/23 14:02
 */
@Repository
@Slf4j
public class TemplateDaoImpl implements TemplateDao {
    private static final Integer DEFAULT_SEQ = 0;
    @Autowired
    private TicketTemplateV2DaoMapper ticketTemplateV2DaoMapper;

    @Override
    public List<TicketTemplateV2> listTemplate(Integer type, PageParam pageParam, String orgId, boolean listAllFlag) {
        Integer skip = null;
        Integer size = null;
        if (!listAllFlag) {
            Integer page = pageParam.getPage();
            size = pageParam.getSize();
            skip = (page - 1) * size;
        }

        List<TicketTemplateV2DO> list = ticketTemplateV2DaoMapper.listByOrgIdAndType(orgId, type, skip, size);

        return TicketTemplateV2Converter.doListToDtoList(list);
    }

    @Override
    public TicketTemplateV2 insertTemplate(TicketTemplateV2 ticketTemplateV2) {
        TicketTemplateV2DO entityDo = TicketTemplateV2Converter.dtoToDo(ticketTemplateV2);
        if (Objects.isNull(entityDo)) {
            return null;
        }

        if (Objects.isNull(entityDo.getTemplateId())) {
            entityDo.setTemplateId(ObjectId.next());
        }
        if (Objects.isNull(entityDo.getCreateTime())) {
            entityDo.setCreateTime(DateUtil.getCommonDate());
        }
        if (Objects.isNull(entityDo.getUpdateTime())) {
            entityDo.setUpdateTime(DateUtil.getCommonDate());
        }

        entityDo.setUsed(TemplateUsedEnum.TEMPLATE_UNUSED.getIndex());

        ticketTemplateV2DaoMapper.insert(entityDo);

        entityDo = ticketTemplateV2DaoMapper.findByTemplateId(entityDo.getTemplateId());

        return TicketTemplateV2Converter.doToDto(entityDo);
    }

    /**
     * 批量保存模板
     *
     * @param ticketTemplateV2List 模板列表
     * @return List<TicketTemplateV2> 成功列表
     * @author jy.zhao
     * @date 2021/11/10 17:07
     **/
    @Override
    public List<TicketTemplateV2> batchInsertTemplate(List<TicketTemplateV2> ticketTemplateV2List) {
        List<TicketTemplateV2> result = new ArrayList<>();
        for (TicketTemplateV2 ticketTemplateV2 : ticketTemplateV2List) {
            TicketTemplateV2DO entityDo = TicketTemplateV2Converter.dtoToDo(ticketTemplateV2);
            if (Objects.isNull(entityDo)) {
                return null;
            }

            if (Objects.isNull(entityDo.getTemplateId())) {
                entityDo.setTemplateId(ObjectId.next());
            }
            if (Objects.isNull(entityDo.getCreateTime())) {
                entityDo.setCreateTime(DateUtil.getCommonDate());
            }
            if (Objects.isNull(entityDo.getUpdateTime())) {
                entityDo.setUpdateTime(DateUtil.getCommonDate());
            }

            entityDo.setUsed(TemplateUsedEnum.TEMPLATE_UNUSED.getIndex());

            ticketTemplateV2DaoMapper.insert(entityDo);
            result.add(TicketTemplateV2Converter.doToDto(entityDo));
        }
        return result;
    }

    @Override
    public TicketTemplateV2 getTemplateById(String templateId) {
        TicketTemplateV2DO ticketTemplateV2DO = ticketTemplateV2DaoMapper.findByTemplateId(templateId);

        return TicketTemplateV2Converter.doToDto(ticketTemplateV2DO);
    }

    @Override
    public TicketTemplateV2 getTemplateByName(String orgId, String name) {

        TicketTemplateV2DO ticketTemplateV2DO = ticketTemplateV2DaoMapper.findByOrgIdAndName(orgId, name);

        return TicketTemplateV2Converter.doToDto(ticketTemplateV2DO);
    }

    @Override
    public Integer deleteTemplateById(String templateId) {
        return ticketTemplateV2DaoMapper.deleteByTemplateId(templateId);
    }

    @Override
    public long updateTemplate(TicketTemplateV2 ticketTemplateV2, boolean updateTime) {
        TicketTemplateV2DO entityDo = TicketTemplateV2Converter.dtoToDo(ticketTemplateV2);
        if (Objects.isNull(entityDo)) {
            return 0L;
        }
        if (updateTime) {
            entityDo.setUpdateTime(DateUtil.getCommonDate());
        }
        if (log.isInfoEnabled()) {
            log.info("[Ticket] 复制提模板：param={},id{}", entityDo.getSample(), entityDo.getTemplateId());
        }
        return ticketTemplateV2DaoMapper.updateByTemplateId(entityDo);
    }

    /**
     * 得到最近使用的模板
     *
     * @param orgId
     * @param userId
     * @return
     */
    @Override
    public TicketConfig getRecentUseTemplate(String orgId, String userId) {
        // 220114时, 没人使用这个方法, 就不写实现了
        return null;
    }

    @Override
    public long getTemplateCount(Integer type, String orgId) {
        return ticketTemplateV2DaoMapper.countByOrgIdAndType(orgId, type);
    }

    @Override
    public TicketTemplateV2 getTemplateByIdAndOrgId(String id, String orgId) {
        TicketTemplateV2DO ticketTemplateV2DO = ticketTemplateV2DaoMapper.findByTemplateIdAndOrgId(id, orgId);

        return TicketTemplateV2Converter.doToDto(ticketTemplateV2DO);
    }

    @Override
    public List<TicketTemplateV2> getTemplateByOrgIdAndName(String orgId, String name) {
        List<TicketTemplateV2DO> ticketTemplateV2DOList = ticketTemplateV2DaoMapper.listByOrgIdAndName(orgId, name);

        return TicketTemplateV2Converter.doListToDtoList(ticketTemplateV2DOList);
    }

    @Override
    public Long updateTemplateName(String id, String name, String userId) {
        TicketTemplateV2DO entityDo = new TicketTemplateV2DO();
        entityDo.setTemplateId(id);
        entityDo.setName(name);
        entityDo.setOperatorId(userId);

        entityDo.setUpdateTime(DateUtil.getCommonDate());

        return ticketTemplateV2DaoMapper.updateByTemplateId(entityDo);
    }

    @Override
    public List<TicketTemplateV2> listTemplateByIds(List<String> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }

        List<TicketTemplateV2DO> ticketTemplateV2DOList = ticketTemplateV2DaoMapper.listByTemplateIdList(ids);

        return TicketTemplateV2Converter.doListToDtoList(ticketTemplateV2DOList);
    }

    @Override
    public List<TicketTemplateV2> getDefaultTemplate(String orgId) {
        List<TicketTemplateV2DO> ticketTemplateV2DOList = ticketTemplateV2DaoMapper.listByOrdIdAndSeq(orgId, DEFAULT_SEQ);

        return TicketTemplateV2Converter.doListToDtoList(ticketTemplateV2DOList);
    }

    @Override
    public TicketTemplateV2 getDefaultTemplate(String orgId, Integer type) {
        TicketTemplateV2DO ticketTemplateV2DO = ticketTemplateV2DaoMapper.findByOrdIdAndSeqAndType(orgId, DEFAULT_SEQ, type);

        return TicketTemplateV2Converter.doToDto(ticketTemplateV2DO);
    }

    /**
     * 查询 findByConfigId 信息
     *
     * @param configId
     * @return TicketTemplate
     * @version v1.0
     * @author gy.liu
     * @date 2021/8/17
     */
    @Override
    public TicketTemplateV2 findByConfigId(String configId) {
        // 220114时, 没人使用这个方法, 就不写实现了
        return null;
    }

    @Override
    public List<TicketTemplateV2> listByTicketConfigId(String ticketConfigId) {
        List<TicketTemplateV2DO> ticketTemplateV2DOList = ticketTemplateV2DaoMapper.listByTicketConfigId(ticketConfigId);

        return TicketTemplateV2Converter.doListToDtoList(ticketTemplateV2DOList);
    }
}
